home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 7: Sunsite / Linux Cubed Series 7 - Sunsite Vol 1.iso / system / filesyst / thsfs.tgz / thsfs.tar / thsfs / fat.c < prev    next >
C/C++ Source or Header  |  1994-10-04  |  4KB  |  229 lines

  1. /*************************************************************
  2.  *                                                           *
  3.  *    ths  Filesystem                  04.10.94      V1.1    *
  4.  *                                                           *
  5.  *    Thomas Scheuermann     ths@ai-lab.fh-furtwangen.de     *
  6.  *                                                           *
  7.  *************************************************************/
  8.  
  9. #include <linux/kernel.h>
  10. #include <linux/module.h>
  11. #include <linux/sched.h>
  12. #include <linux/errno.h>
  13. #include <linux/string.h>
  14. #include <linux/stat.h>
  15. #include <linux/mm.h>
  16. #include <linux/locks.h>
  17. #include <linux/fs.h>
  18. #include <linux/malloc.h>
  19. #include "ths.h"
  20. #include "ths_i.h"
  21.  
  22.  
  23. void fatfree(struct super_block *s)
  24. {
  25.     int i;
  26.     struct ths_sb_info *ths_sb;
  27.  
  28.     ths_sb = (struct ths_sb_info *)s->u.generic_sbp;
  29.  
  30. #ifdef DEBUG
  31.     printk("fatfree\n");
  32. #endif
  33. /*
  34.  * Speicher der Clusteradressen freigeben
  35.  */
  36.  
  37.     for(i=0;i<ths_sb->AnzahlCluster+2;)
  38.     {
  39.         kfree_s(ths_sb->fat[i/2048],4096);
  40.         i+=2048;
  41.     }
  42. }
  43.  
  44. void fatmem2(struct super_block *s)
  45. {
  46.     struct ths_sb_info *ths_sb;
  47.     struct ths_buffer tbf;
  48.     int i,k,t;
  49.     unsigned short cl1,cl2,cluster,addr;
  50.     unsigned char *data=NULL;
  51.  
  52.     ths_sb = (struct ths_sb_info *)s->u.generic_sbp;
  53.  
  54. #ifdef DEBUG
  55.     printk("fatmem2\n");
  56. #endif
  57.  
  58. /*
  59.  * Speicher fuer alle Clusteradressen holen
  60.  */
  61.  
  62.     for(i=0;i<ths_sb->AnzahlCluster+2;)
  63.     {
  64.         ths_sb->fat[i/2048] = (short *)kmalloc(4096,GFP_KERNEL);
  65.         i+=2048;
  66.     }
  67.  
  68. #ifdef DEBUG
  69.     printk("Speicher erhalten : %d\n",i);
  70. #endif
  71.  
  72.  
  73. /*
  74.  * k    : Nibble-Zaehler
  75.  * addr : Adresse des Speicherblocks
  76.  * i    : Byte vom Clustersektor
  77.  * addr : Adresse im FAT-Speicher = Clusternummer
  78.  */
  79.     t = ths_sb->BitsProCluster/4;
  80.     cl1 = 0;
  81.     cl2 = 0;
  82.     addr = 0;
  83.     k = 0;
  84.  
  85.     for(i=0,addr=0;addr<(ths_sb->AnzahlCluster+2);)
  86.     {
  87.         if(!(i & 0x1ff))
  88.         {
  89.             if(ths_read_sektor(s,ths_sb->FatStart+(i>>9),&tbf))
  90.             {
  91.                 printk("Fehler\n");
  92.                 return;
  93.             }
  94.             data = tbf.data[0];
  95.         }
  96.         cl1 = (short)data[i&0x1ff];
  97.         k=(k+1)%t;
  98.         if(!k)
  99.         {
  100.             cluster = (cl2 | ((cl1<<8)&0x0fff));
  101.             (ths_sb->fat[addr/2048])[addr%2048] = cluster;
  102.             addr++;
  103.         }
  104.         k=(k+1)%t;
  105.         if(!k)
  106.         {
  107.             if(t==3)
  108.             {
  109.                 cluster = ((cl2 & 0xf0) >> 4) | (cl1 << 4);
  110.             }
  111.             else
  112.             {
  113.                 cluster = cl2 | (cl1 << 8);
  114.             }
  115.             (ths_sb->fat[addr / 2048])[addr % 2048] = cluster;
  116.             addr++;
  117.         }
  118.         cl2 = cl1;
  119.         i++;
  120.         if(!(i & 0x1ff))
  121.             ths_free_sektor(&tbf);
  122.     }
  123.     if(i & 0x1ff)
  124.         ths_free_sektor(&tbf);
  125. }
  126.  
  127.  
  128. void cvffill(struct super_block *s)
  129. {
  130.     struct ths_sb_info *ths_sb;
  131.     struct buffer_head *fbh;
  132.     int i,j,clb;
  133.     unsigned short cl1,cluster,test;
  134.     unsigned char *data=NULL,b1=0;
  135.     unsigned long fatsektor;
  136.  
  137.     ths_sb = (struct ths_sb_info *)s->u.generic_sbp;
  138.  
  139. #ifdef DEBUG
  140.     printk("cvffill\n");
  141. #endif
  142.  
  143.     cluster  = ths_sb->StartCluster;
  144.     cl1 = cluster;
  145.     ths_sb->cvf[0] = NULL;
  146.     if(ths_sb->uBitsProCluster==12)
  147.         test = 0x0fff;
  148.     else
  149.         test = 0xffff;
  150.     for(i=0;cl1!=test;)
  151.     {
  152.         clb = (cluster*(ths_sb->uBitsProCluster>>2));
  153.         fatsektor = ths_sb->uFATStart + ((clb>>1)/512);
  154.         fbh = bread(s->s_dev,fatsektor>>1,BLOCK_SIZE);
  155.  
  156.         j=0;
  157.         if(fatsektor&1)
  158.             j=512;
  159.         data = &(fbh->b_data[j]);
  160.         b1 = data[(clb>>1) & 0x1ff];
  161.         if(clb & 1)
  162.             cluster = (unsigned short)(b1 & 0xf0)>>4;
  163.         else
  164.             cluster = (unsigned short)b1;
  165.  
  166.         if(((clb>1) & 0x1ff)==511)
  167.         {
  168.             brelse(fbh);
  169.             fbh = bread(s->s_dev,(fatsektor+1)>>1,BLOCK_SIZE);
  170.             j=0;
  171.             if((fatsektor+1)&1)
  172.                 j=512;
  173.             data = &(fbh->b_data[j]);
  174.         }
  175.  
  176.         b1 = data[((clb>>1)+1) & 0x1ff];
  177.         if(clb & 1)
  178.             cluster |= (unsigned short)b1  << 4;
  179.         else
  180.             cluster |= (unsigned short)b1 << 8;
  181.  
  182.         if(ths_sb->uBitsProCluster == 12)
  183.             cluster &=0xfff;
  184.         brelse(fbh);
  185.         if(cluster != cl1+1)
  186.         {
  187.             if(!(i%2048))
  188.             {
  189.                 ths_sb->cvf[i/2048] = (short *)kmalloc(4096,GFP_KERNEL);
  190.                 ths_sb->cvf[i/2048+1] = NULL;
  191.             }
  192.             (ths_sb->cvf[i/2048])[i%2048] = cl1;
  193.             i++;
  194.             (ths_sb->cvf[i/2048])[i%2048] = cluster;
  195.             i++;
  196. #ifdef DEBUG
  197.             printk("Nach %d folgt %d\n",cl1,cluster);
  198. #endif
  199.         }
  200.         cl1 = cluster;
  201.     }
  202. #ifdef DEBUG
  203.     printk("Luecken : %d\n",i/2);
  204. #endif
  205.  
  206. }
  207.  
  208.  
  209. void cvffree(struct super_block *s)
  210. {
  211.     int i;
  212.     struct ths_sb_info *ths_sb;
  213.  
  214.     ths_sb = (struct ths_sb_info *)s->u.generic_sbp;
  215.  
  216. #ifdef DEBUG
  217.     printk("cvffree\n");
  218. #endif
  219. /*
  220.  * Speicher der Clusteradressen freigeben
  221.  */
  222.  
  223.     for(i=0;ths_sb->cvf[i/2048]!=NULL;)
  224.     {
  225.         kfree_s(ths_sb->cvf[i/2048],4096);
  226.         i+=2048;
  227.     }
  228. }
  229.